package com.iuie.ns.frame.utils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import net.sf.cglib.beans.BeanMap;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public class BeanUtils {

	private BeanUtils() {

	}

	/**
	 * 将对象装换为map
	 * 
	 * @param bean
	 * @return
	 */
	public static <T> Map<String, Object> beanToMap(T bean) {
		Map<String, Object> map = Maps.newHashMap();
		if (bean != null) {
			BeanMap beanMap = BeanMap.create(bean);
			for (Object key : beanMap.keySet()) {
				map.put(key + "", beanMap.get(key));
			}
		}
		return map;
	}

	/**
	 * 将map装换为javabean对象
	 * 
	 * @param map
	 * @param bean
	 * @return
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public static <T> T mapToBean(Map<String, Object> map, T bean) throws NoSuchFieldException, SecurityException, InstantiationException, IllegalAccessException {
	    map = correctDataType(map, bean);
		BeanMap beanMap = BeanMap.create(bean);
		beanMap.putAll(map);
		return bean;
	}

	/**
	 * 纠正数据类型
	 * @param map
	 * @param bean
	 * @return
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @date 2017年7月2日 下午3:03:55
	 */
	private static <T> Map<String, Object> correctDataType(Map<String, Object> map, T bean) throws NoSuchFieldException, SecurityException, InstantiationException, IllegalAccessException {
		Map<String, Object> newMap = new HashMap<String, Object>(); // 新的
	    Iterator<Entry<String, Object>> it = map.entrySet().iterator();
	    while(it.hasNext()) {
	        Entry<String, Object> en = it.next();
	        String name = en.getKey();
	        
	        String[] xy = null;
	        if (StringUtils.isNotBlank(name) && name.contains(".")) {
	        	// x.y
	        	xy = StringUtils.split(name, ".");
	        	name = xy[0];
	        }
	        Class<?> clazz = ReflectionUtils.getDeclaredFieldType(name, bean.getClass());
	        Object value = WrapperTypeValueConvertUtils.convert(clazz, en.getValue());
	        if (value != null) {
	        	if (value.getClass() == clazz) {
	        		newMap.put(name, value);
	        	} else if (xy != null && xy.length > 1){
	        		// 针对x.y情形设计
	        		Object obj = clazz.newInstance();
	        		Field field = ReflectionUtils.findField(clazz, xy[1]);
	        		field.setAccessible(true);
	        		field.set(obj, value);
	        		newMap.put(name, obj);
	        	}
	        }
	    }
        return newMap;
	}
	
	/**
	 * 将List<T>转换为List<Map<String, Object>>
	 * 
	 * @param objList
	 * @return
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public static <T> List<Map<String, Object>> objectsToMaps(List<T> objList) {
		List<Map<String, Object>> list = Lists.newArrayList();
		if (objList != null && objList.size() > 0) {
			Map<String, Object> map = null;
			T bean = null;
			for (int i = 0, size = objList.size(); i < size; i++) {
				bean = objList.get(i);
				map = beanToMap(bean);
				list.add(map);
			}
		}
		return list;
	}

	/**
	 * 将List<Map<String,Object>>转换为List<T>
	 * 
	 * @param maps
	 * @param clazz
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 */
	public static <T> List<T> mapsToObjects(List<Map<String, Object>> maps,
			Class<T> clazz) throws InstantiationException,
			IllegalAccessException, NoSuchFieldException, SecurityException {
		List<T> list = Lists.newArrayList();
		if (maps != null && maps.size() > 0) {
			Map<String, Object> map = null;
			T bean = null;
			for (int i = 0, size = maps.size(); i < size; i++) {
				map = maps.get(i);
				bean = clazz.newInstance();
				mapToBean(map, bean);
				list.add(bean);
			}
		}
		return list;
	}
}
